// pages/course/search/search.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setInterval:false,
    pageNum:1,
    pageSize:10,
    url: '',
    courseList: [],
    keyword: ''
  },
  gojingjiangCourse:function(e){
    const id=e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
     url:'/pages/course/jingjiangCourse/jingjiangCourse?id='+id,
    });
  },
  onInputChange:function(e){
    this.setData({
      keyword:e.detail.value
    });
    this.searchCourses();
  },
  searchCourses: function () {
    let that = this;
    let url;
    if (that.data.keyword || that.data.keyword=='') {
      // 有输入内容时，进行模糊查询
      url = that.data.url + 'course/selectcoursebyname?courseName=' + encodeURIComponent(that.data.keyword) + '&pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize;
    } else {
      // 无输入内容时，进行全部查询
      url = that.data.url + 'course/findAll?pageNum=' + that.data.pageNum + '&pageSize=' + that.data.pageSize;
    }
    wx.request({
      url: url,
      success(resp) {
        if (resp.data && resp.data.data) {
          console.log(resp.data.data.list);
          that.setData({
            courseList: resp.data.data.list,
          });
        } else {
          console.log(resp.data.data);
        }
      },
      complete() {
        // 请求完成后的处理
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      url:app.globalData.URL
       })
       this.searchCourses();
       // 动态调用后端接口 
   this.searchCourses();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNum:1,
      pageSize:10,
      courseList: []
    })
    this.searchCourses();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that=this;
    that.setData({
      pageSize:that.data.pageSize+5,
      setInterval:true
    });
      setTimeout(function () {
        that.setData({
          setInterval:false
        });
          }, 2000); 
          this.searchCourses();
    wx.request({
      url: that.data.url+'course/findAll?pageNum='+that.data.pageNum+'&pageSize='+that.data.pageSize,
      success(resp){
        that.setData({
          courseList:resp.data.data.list
        })
        }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goCourse(){
    wx:wx.switchTab({
      url: '/pages/course/course',
    })
  }
})